<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class CommentAction extends BaseApiAction {

	public function index() {
		die('非法参数');
	}

	//添加评论
	public function save() {
		$this->auth();
		$data['user_id'] = $this->uid;
		//$data['school_id'] = $_POST['school_id'];
		$data['tweets_id'] = $_POST['tweets_id'];
		$data['parent_id'] = $_POST['parent_id'];
		empty($data['tweets_id']) && $this->ajaxReturn('', '该主题不存在或被删除!', -2);
		trim($_POST['content']) == "" && $this->ajaxReturn('', '请填写评论内容!', -1);

		$Comment = D('Comment');
		$comment = $Comment->where(array(
				"user_id" => $data["user_id"],
				"tweets_id" => $data["tweets_id"],
			))->find();
		//判断用户是否楼主
		$tweets = D("Tweets")->find($data['tweets_id']);
		if ($tweets["user_id"] == $data['user_id']) {
			$data['nickname'] = $tweets["nickname"];
		} else {
			//若不是楼主，判断用户是否评论过
			if ($comment) {
				//获取用户在该主题中的昵称
				$data['nickname'] = $comment['nickname'];
			} else {
				//获取一个随机不重用的昵称
				$data['nickname'] = get_rand_name_by_tweetsId($data['tweets_id']);
			}
		}
		$data['content'] = text_to_html($_POST['content']);
		$data['school_id'] = $tweets["school_id"];
		$data['create_time'] = time();
		$data['status'] = 1;
		//添加评论
		$data['comment_id'] = $Comment->data($data)->add();
		if ($data['comment_id']) {
			D("Tweets")->where("tweets_id=" . $data['tweets_id'])->setInc("comment_count", 1);
			//添加提醒
			if ($data['parent_id']) {
				if ($this->uid != $comment['user_id']) {
					notice_log($comment['user_id'], 3, $this->school["school_id"], $data['tweets_id'], array(
						"comment_id" => $data['comment_id'],
					));
				}
			} else {
				if ($this->uid != $tweets['user_id']) {
					notice_log($tweets['user_id'], 2, $this->school["school_id"], $data['tweets_id'], array(
					));
				}
			}
			if ($data["user_id"] == $tweets["user_id"]) {
				$data['nickname'] = "楼主";
			}
			if ((int) $data["parent_id"] != 0) {
				$data['content'] = preg_replace("/to (\d+F)\:/i", "to <abbr class=\"comment_object\" data-parent-id=\"" . $data["parent_id"] . "\" title=\"回复$1的评论\">$1</abbr>:", $data['content']);
			}
			$data['create_time'] = "刚刚";
			$this->ajaxReturn($data, '回复成功!', 1);
		} else {
			$this->ajaxReturn(null, '回复失败!', 0);
		}
	}

	//获取评论
	public function lists() {
		$start = $_GET["start"] ? $_GET["start"] : 0;
		$limit = $_GET["limit"] ? $_GET["limit"] : 20;
		$data['tweets_id'] = $_GET["tweets_id"];
		if ($start > 0) {
			$data['comment_id'] = array('lt', $start);
		}
		$comment = D("Comment")->where($data)->order("comment_id desc")->limit("0," . $limit)->select();
		if ($comment) {
			$tweets = D("Tweets")->find($comment[0]['tweets_id']);
			foreach ($comment as $key => $val) {
				if ($val["user_id"] == $tweets["user_id"])
					$comment[$key]['nickname'] = "楼主";
				if ($val["status"] == 0 && $val['user_id'] != $this->uid) {
					$comment[$key]["content"] = "该评论已被删除！";
				}
				if ((int) $comment[$key]["parent_id"] != 0) {
					$comment[$key]['content'] = preg_replace("/to (\d+F)\:/i", "to <abbr class=\"comment_object\" data-parent-id=\"" . $comment[$key]["parent_id"] . "\" title=\"回复$1的评论\">$1</abbr>:", $comment[$key]['content']);
				}
				$comment[$key]["create_time"] = friendly_date($val["create_time"], "normal");
			}
		}
		//$comment = array_reverse($comment);
		$this->ajaxReturn($comment, "", 1);
	}

	//获取单条评论信息
	public function get() {
		$comment_id = (int) $_GET["comment_id"];
		$comment = D("Comment")->where("comment_id=" . $comment_id)->find();
		if ($comment)
			$comment["create_time"] = friendly_date($comment["create_time"], "normal");
		$this->ajaxReturn($comment, "", 1);
	}

	//删除评论
	public function delete() {
		$this->auth();
		$comment_id = (int) $_GET["comment_id"];
		$comment = D("Comment")->where("comment_id=" . $comment_id . " and user_id=" . $this->uid)->setField("status", 0);
		if ($comment) {
			$this->ajaxReturn($comment, "删除成功", 1);
		} else {
			$this->ajaxReturn(0, "您没有删除该条评论的权限", 0);
		}
	}

}

?>
